%
% fm3tr_monte_carlo.m
%
% FM3TR Simulator for 2-stage Reed-Solomon Encoder and Decoder
%
% 
% Tansu Demirbilek
% ademirbi@mc.com
% Mercury Computer Systems

clear all;
close all;
clc;
BER = logspace(-2, -6, 18)'; % vector for input BER values
N_Trials = 2;              % number of trials over which to average
numBlockCodes = 2; %must be even
errors = zeros(1,length(BER));

disp('FM3TR Simulator for 2-stage Reed-Solomon Encoder and Decoder');
disp('---------------');
disp(['BER points: ' num2str(length(BER))]);
disp(['Trials: ' num2str(N_Trials)]);
disp('---------------');

format long;

for i=1:length(BER)
    fprintf('BER = %f [%d/%d]\n',BER(i),i,length(BER));
    for j=1:N_Trials
        fprintf('  [%d/%d], total errors: ',j,N_Trials);
        errors(i) = fm3tr_code(BER(i),numBlockCodes) + errors(i);
        fprintf('%d\n',errors(i));
        
    end
end


% Average out the errors
errors = errors./(numBlockCodes*N_Trials);

% Plot the results
figure;
semilogx(BER,errors,'*-');
xlabel('Bit Error Rate');
ylabel('Probability of decoding error of a block');
title('BER vs. FM3TR RS Coder Performance');

figure;
BLER = 1-(1-BER).^16; % nHop = 16
semilogx(BLER,errors,'*r-');
xlabel('Block Error Rate (BLER)');
ylabel('Probability of decoding error of a block');
title('BLER vs. FM3TR RS Coder Performance');

